package com.web.employees;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.domain.Employees;
import com.service.LoginService;
import com.util.AuthenticateHttpServlet;
import com.util.AuthenticateSession;

/**
 * Servlet implementation class EmployeeLogin
 */
@WebServlet("/Employee/Login")
public class EmployeeLogin extends AuthenticateHttpServlet {
	private static final long serialVersionUID = 1L;

	protected void post(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String userId = request.getParameter("name");
		String password = request.getParameter("passwd");
		LoginService login = new LoginService();
		Employees e = login.login(userId, password);
		if (e != null) {
			HttpSession session = request.getSession();
			AuthenticateSession auth = new AuthenticateSession();
			auth.setUsername(e.getUsername());
			auth.setUserId(e.getEmployeeId());
			session.setAttribute("authenticateSession", auth);
			request.getRequestDispatcher("/index").forward(request, response);
		} else {
			request.getRequestDispatcher("/login.jsp").forward(request, response);

		}
	}

}
